<script setup lang="ts">
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { login } from "@/api/user/register.ts";
import { ElMessage } from "element-plus";
import { useAuthStore } from '@/stores/auth'
import { User, Lock, ArrowRight } from '@element-plus/icons-vue'

const username = ref('')
const password = ref('')
const loading = ref(false)
const router = useRouter()
const authStore = useAuthStore()

const handleLogin = async () => {
  if (!username.value.trim()) {
    ElMessage.warning('请输入用户名')
    return
  }
  if (!password.value.trim()) {
    ElMessage.warning('请输入密码')
    return
  }

  try {
    loading.value = true
    const res = await login(username.value, password.value);
    if (res.status === 201 || res.status === 200) {
      authStore.login(res.data);
      ElMessage.success('登录成功');
      setTimeout(() => {
        router.push('/home');
      }, 1000);
    }
  } catch (e: any) {
    const errorText = e?.response?.data?.message || '登录失败，请检查用户名和密码';
    ElMessage.error(errorText);
  } finally {
    loading.value = false
  }
}

const goToRegister = () => {
  router.push('/register');
}
</script>

<template>
  <div class="auth-page">
    <!-- 背景装饰 -->
    <div class="bg-decoration">
      <div class="bg-shape bg-shape-1"></div>
      <div class="bg-shape bg-shape-2"></div>
      <div class="bg-shape bg-shape-3"></div>
    </div>

    <div class="auth-container">
      <!-- 左侧装饰区域 -->
      <div class="auth-banner">
        <div class="banner-content">
          <div class="banner-icon">📚</div>
          <h1>图书管理系统</h1>
          <p>简单高效的个人图书管理工具</p>
          <div class="banner-features">
            <div class="feature-item">
              <div class="feature-icon">➕</div>
              <span>添加图书</span>
            </div>
            <div class="feature-item">
              <div class="feature-icon">📝</div>
              <span>编辑信息</span>
            </div>
            <div class="feature-item">
              <div class="feature-icon">🔍</div>
              <span>快速查找</span>
            </div>
            <div class="feature-item">
              <div class="feature-icon">🗑️</div>
              <span>删除管理</span>
            </div>
          </div>
        </div>
      </div>

      <!-- 右侧登录表单 -->
      <div class="auth-form-container">
        <div class="auth-form">
          <div class="form-header">
            <h2>欢迎回来</h2>
            <p>请登录您的账户</p>
          </div>

          <form @submit.prevent="handleLogin" class="login-form">
            <div class="form-group">
              <div class="input-wrapper">
                <el-icon class="input-icon"><User /></el-icon>
                <input
                    type="text"
                    v-model="username"
                    placeholder="请输入用户名"
                    class="form-input"
                    required
                    :disabled="loading"
                />
              </div>
            </div>

            <div class="form-group">
              <div class="input-wrapper">
                <el-icon class="input-icon"><Lock /></el-icon>
                <input
                    type="password"
                    v-model="password"
                    placeholder="请输入密码"
                    class="form-input"
                    required
                    :disabled="loading"
                />
              </div>
            </div>

            <button
                type="submit"
                class="submit-btn"
                :disabled="loading"
                :class="{ 'loading': loading }"
            >
              <span v-if="!loading" class="btn-content">
                <span>立即登录</span>
                <el-icon class="btn-icon"><ArrowRight /></el-icon>
              </span>
              <span v-else class="btn-loading">
                <div class="loading-spinner"></div>
                <span>登录中...</span>
              </span>
            </button>
          </form>

          <div class="form-footer">
            <div class="divider">
              <span>还没有账号？</span>
            </div>
            <button @click="goToRegister" class="link-btn">
              立即注册
            </button>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped>
.auth-page {
  min-height: 100vh;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  position: relative;
  overflow: hidden;
}

.bg-decoration {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  pointer-events: none;
}

.bg-shape {
  position: absolute;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.1);
  animation: float 6s ease-in-out infinite;
}

.bg-shape-1 {
  width: 200px;
  height: 200px;
  top: 10%;
  left: 10%;
  animation-delay: 0s;
}

.bg-shape-2 {
  width: 150px;
  height: 150px;
  top: 60%;
  right: 20%;
  animation-delay: 2s;
}

.bg-shape-3 {
  width: 100px;
  height: 100px;
  bottom: 20%;
  left: 60%;
  animation-delay: 4s;
}

@keyframes float {
  0%, 100% { transform: translateY(0px); }
  50% { transform: translateY(-20px); }
}

.auth-container {
  max-width: 1000px;
  width: 100%;
  background: white;
  border-radius: 20px;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.2);
  overflow: hidden;
  display: grid;
  grid-template-columns: 1fr 1fr;
  min-height: 600px;
}

.auth-banner {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  padding: 60px 40px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  position: relative;
}

.banner-content {
  text-align: center;
  max-width: 300px;
}

.banner-icon {
  font-size: 64px;
  margin-bottom: 24px;
  display: block;
}

.banner-content h1 {
  font-size: 28px;
  font-weight: 700;
  margin-bottom: 16px;
  line-height: 1.2;
}

.banner-content p {
  font-size: 16px;
  opacity: 0.9;
  margin-bottom: 40px;
  line-height: 1.6;
}

.banner-features {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.feature-item {
  display: flex;
  align-items: center;
  gap: 12px;
  font-size: 14px;
  opacity: 0.9;
}

.feature-icon {
  font-size: 20px;
}

.auth-form-container {
  padding: 60px 40px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.auth-form {
  width: 100%;
  max-width: 320px;
}

.form-header {
  text-align: center;
  margin-bottom: 40px;
}

.form-header h2 {
  font-size: 28px;
  font-weight: 700;
  color: #2d3748;
  margin-bottom: 8px;
}

.form-header p {
  color: #718096;
  font-size: 14px;
}

.login-form {
  margin-bottom: 32px;
}

.form-group {
  margin-bottom: 24px;
}

.input-wrapper {
  position: relative;
  display: flex;
  align-items: center;
}

.input-icon {
  position: absolute;
  left: 16px;
  color: #a0aec0;
  font-size: 18px;
  z-index: 2;
}

.form-input {
  width: 100%;
  padding: 16px 16px 16px 48px;
  border: 2px solid #e2e8f0;
  border-radius: 12px;
  font-size: 14px;
  transition: all 0.3s ease;
  background: #f7fafc;
}

.form-input:focus {
  outline: none;
  border-color: #667eea;
  background: white;
  box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
}

.form-input:disabled {
  background: #f1f5f9;
  cursor: not-allowed;
}

.submit-btn {
  width: 100%;
  padding: 16px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  border: none;
  border-radius: 12px;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.3s ease;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
}

.submit-btn:hover:not(:disabled) {
  transform: translateY(-2px);
  box-shadow: 0 10px 30px rgba(102, 126, 234, 0.4);
}

.submit-btn:disabled {
  opacity: 0.7;
  cursor: not-allowed;
  transform: none;
}

.btn-content {
  display: flex;
  align-items: center;
  gap: 8px;
}

.btn-icon {
  font-size: 16px;
}

.btn-loading {
  display: flex;
  align-items: center;
  gap: 8px;
}

.loading-spinner {
  width: 16px;
  height: 16px;
  border: 2px solid rgba(255, 255, 255, 0.3);
  border-top: 2px solid white;
  border-radius: 50%;
  animation: spin 1s linear infinite;
}

@keyframes spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}

.form-footer {
  text-align: center;
}

.divider {
  margin-bottom: 16px;
  color: #718096;
  font-size: 14px;
}

.link-btn {
  background: none;
  border: none;
  color: #667eea;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  padding: 8px 16px;
  border-radius: 8px;
  transition: all 0.3s ease;
}

.link-btn:hover {
  background: rgba(102, 126, 234, 0.1);
  color: #5a67d8;
}

@media (max-width: 768px) {
  .auth-container {
    grid-template-columns: 1fr;
    max-width: 400px;
  }

  .auth-banner {
    padding: 40px 20px;
    min-height: auto;
  }

  .banner-content h1 {
    font-size: 24px;
  }

  .banner-features {
    flex-direction: row;
    justify-content: center;
    flex-wrap: wrap;
  }

  .auth-form-container {
    padding: 40px 20px;
  }
}
</style>